<!--
 * @Description: 
 * @Date: 2024-05-30 10:46:11
 * @LastEditTime: 2024-06-06 15:35:01
-->
<template>
  <Dialog :title="dialogTitle" v-model="auditVisible">
    <el-form ref="formRef" :model="formData" :rules="formRules" label-width="80px">
      <el-form-item label="审核结果" prop="status">
        <el-radio-group v-model="formData.status">
          <el-radio value="2">通过</el-radio>
          <el-radio value="3">驳回</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="审核说明" prop="acceptContent">
        <el-input v-model="formData.acceptContent" type="textarea" :autosize="{ minRows: 5 }" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="auditVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import * as CaseQueryRecordApi from '@/api/pro/casequeryrecord'
import * as intervieWappointmentApi from '@/api/pro/interviewappointment'
import * as PetitionRequestRecordApi from '@/api/pro/petitionrequestrecord'
import * as DetentionReviewCenterApi from '@/api/pro/detentionreviewcenter'
import * as PublicInterestLitigationApi from '@/api/pro/publicinterestlitigation'

import { useUserStore } from '@/store/modules/user'
const dialogTitle = ref('案件审核') // 弹窗的标题
const auditVisible = ref(false)
const message = useMessage()
const emit = defineEmits(['success'])
const props = defineProps(['type'])
const userStore = useUserStore()
const formData = ref({
  id: undefined,
  status: undefined,
  acceptContent: undefined,
  acceptUserName:userStore.user.nickname
})
const formRules = reactive({
  status: [{ required: true, message: '审核结果不能为空', trigger: 'change' }],
  acceptContent: [{ required: true, message: '审核说明不能为空', trigger: 'blur' }]
})
const formLoading = ref(false)
const formRef = ref() // 表单 Ref
const open = async (id?: number) => {
  auditVisible.value = true
  formData.value.id = undefined
  formData.value.status = undefined
  formData.value.acceptContent = undefined  
  // 修改时，设置数据
  if (id) {
    formData.value.id = id
  }
}
const submitForm = async function () {
  // 校验表单
  if (!formRef) return
  const valid = await formRef.value.validate()
  if (!valid) return
  // 提交请求
  formLoading.value = true
  try {
    const data = { ...formData.value,acceptTime: new Date().getTime() }
    if (props.type == 'jieFang') {
      await intervieWappointmentApi.updateInterviewAppointment(data)
    } else if (props.type == 'xinFang') {
      await PetitionRequestRecordApi.updatePetitionRequestRecord(data)
    } else if (props.type == 'jiYa') {
      await DetentionReviewCenterApi.updateDetentionReviewCenter(data)
    } else if (props.type == 'gongYi') {
      await PublicInterestLitigationApi.updatePublicInterestLitigation(data)
    } else {
      await CaseQueryRecordApi.updateCaseQueryRecord(data)
    }

    message.success('审核完成')
    auditVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}
defineExpose({ open })
</script>
